package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.mongo.UserLike;
import com.itheima.service.mongo.ShowUserService;
import com.itheima.vo.ShowUserVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class ShowUserServiceImpl implements ShowUserService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public List<RecommendUser> ShowUserCards(Long UserId) {
        Query query = new Query(Criteria.where("toUserId").is(UserId)).with(Sort.by(Sort.Order.desc("score")));

        //根据userId查询用户推荐表
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);


        return recommendUsers;
    }

    //卡片不喜欢
    @Override
    public void unLove(Long userId, Long likeUserId) {
        //userId 当前登陆者id likeUserId对方id

        //创建条件对象
        Query query = new Query(Criteria.where("toUserId").is(userId).and("userId").is(likeUserId));

        //如果符合条件 删除此推荐用户
        mongoTemplate.remove(query,RecommendUser.class);
    }

    //卡片喜欢
    @Override
    public void love(UserLike userLike) {
        mongoTemplate.save(userLike);
    }

    //查看是否互相喜欢
    @Override
    public boolean likeEachOther(Long userId, Long likeUserId) {
        Query query1 = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        Query query2 = new Query(Criteria.where("userId").is(likeUserId).and("likeUserId").is(userId));

        boolean exists1 = mongoTemplate.exists(query1, UserLike.class);
        boolean exists2 = mongoTemplate.exists(query2, UserLike.class);

        if (exists1 && exists2){

            return true;
        }


        return false;
    }
}
